package wolftrace_company.study_android_ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import wolftrace_company.study_android_ui.R;

/**
 * Created by SimpleTonOne on 2018/3/31.
 */

public class StartPageActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager viewPager;
    private List<ImageView> list;
    private Button button;
    private LinearLayout linearLayout;
    private int pWidth = 0;
    private View view1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.startpage);
        view1 = findViewById(R.id.select_point);
        linearLayout = findViewById(R.id.ll_guide_point_group);
        viewPager = findViewById(R.id.sTartPageView);
        button = findViewById(R.id.st_btn_new);
        button.setOnClickListener(this);
        initData();
        GuiPager guiPager = new GuiPager();
        viewPager.setAdapter(guiPager);
        viewPager.addOnPageChangeListener(this);
    }


    public void initData() {
        //定义存放图片的id数组
        int[] imageID = {R.mipmap.a, R.mipmap.b,R.mipmap.c};
        //list里存放imageview对象
        list = new ArrayList<>();
        ImageView imageView;
        //定义小圆点
        View view;
        //定义组件组
        LinearLayout.LayoutParams layoutParams;
        for (int i = 0; i < imageID.length; i++) {
            imageView = new ImageView(this);
            imageView.setImageResource(imageID[i]);
            list.add(imageView);
            //对每一页面的小圆点实例化
            view = new View(this);
            //设置其背景颜色为灰色
            view.setBackgroundResource(R.drawable.point_normal);
            layoutParams = new LinearLayout.LayoutParams(10, 10);
            if (i != 0) {
                //设置圆点的左边距
                layoutParams.leftMargin = 10;

            }
            view.setLayoutParams(layoutParams);
            linearLayout.addView(view);
        }
    }

    /**
     *
     * @param position 当前页面的标识 从左到右 从0开始
     * @param positionOffset 当前页面偏移的百分比
     * @param positionOffsetPixels 当前页面偏移的像素位置
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (pWidth == 0)
            pWidth = linearLayout.getChildAt(1).getLeft() - linearLayout.getChildAt(0).getLeft();
        int leltmargin = (int) (pWidth * (position + positionOffset));
        RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) view1
                .getLayoutParams();
        params.leftMargin = leltmargin;
        view1.setLayoutParams(params);
    }
    /**
     *
     * @param position 当前页面的标识 从左到右 从0开始
     *
     *
     */
    @Override
    public void onPageSelected(int position) {

        if (position == list.size() - 1) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                        handler.sendEmptyMessage(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else {
            button.setVisibility(View.GONE);
        }
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1)
            button.setVisibility(View.VISIBLE);
        }
    };
    /**
     *
     * @param state 代表状态 0表示什么都没做 1的时表示正在滑动 2表示滑动完毕
     */
    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.st_btn_new:
                startActivity(new Intent(StartPageActivity.this, MainActivity.class));
        }
    }


    class GuiPager extends PagerAdapter {

        /**
         *
         * @return 返回一共存在几个页面 即list的长度
         */
        @Override
        public int getCount() {
            return list.size();
        }
        /**
         *
         * @param view
         * @param object
         * @return 判断显示的是否是同一张图片，这里我们将两个参数相比较返回即可
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
        /**
         *
         * @param container
         * @param position
         * @param object
         * PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        /**
         *
         * @param container
         * @param position
         * @return 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView加入到ViewGroup中，然后作为返回值返回即可
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = list.get(position);
            container.addView(imageView);
            return imageView;
        }
    }
}
